// 距底 40px 视为在底部
const SCROLL_THRESHOLD = 40;

// 处理滚动逻辑
export function useChatScroll() {
  // 对话页面底部ref
  const sessionBottomRef = ref<HTMLElement | null>(null);
  // 滚动到底部的辅助函数
  const isNearBottom = (el: HTMLElement) =>
    el.scrollHeight - el.scrollTop - el.clientHeight <= SCROLL_THRESHOLD;

  const scrollToBottom = (immediate = false) => {
    const el = sessionBottomRef.value;
    if (!el) return;
    el.scrollTo({
      top: el.scrollHeight,
      behavior: immediate ? "auto" : "smooth",
    });
  };

  return {
    sessionBottomRef,
    scrollToBottom,
    isNearBottom
  };
}
